<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/9/14
 * Time: 16:54
 */

namespace app\www\controller;

use app\common\model\User;
use think\Session;
use think\Controller;
use think\Db;
class Bussiness extends Common
{
    /*商会详情页*/
    public function index(){
        $id=input('id');
        if(empty($id))
        {
            $this->error("您还没有所属商会！");
        }
        $access_token=session::get("access_token");
        $data1=array(
            "module"=>'Bussiness',
            "interface"=>'1001',
            "version"=>'1.0.0',
            "data"=> [
                "bussiness_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("info",$result_info['info']);
            $this->assign("bussiness_id",$id);
            return $this->fetch();
        }
    }
    /*商会列表*/
    public function bussinessList()
    {
        $id=input('id');
        $access_token=session::get("access_token");
        $page=input('page');
        $page_size=input('page_size');
        $page=empty($page)?1:$page;
        $page_size=empty($page_size)?5:$page_size;;
        $data1=array(
            "module"=>'Bussiness',
            "interface"=>'1005',
            "version"=>'1.0.0',
            "data"=> [
                "page"=> $page,
                "page_size"=> $page_size,
                "type"=> 2,
                "access_token"=> $access_token
            ]
        );

        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];

        if(request()->isAjax())
        {
            if($result_code!=="0000")
            {
                return 0;
            }else {
                $result_data = json_decode($res, true)['result_data']['list'];
                /*手机号隐藏中间四位（商会不隐藏）*/
//                foreach($result_data as &$value)
//                {
//                    $value['tel']=substr_replace($value['tel'],'****',3,4);
//                }
                return json_encode($result_data);
            }
        }else{
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }else{
                $result_info=json_decode($res,true)['result_data'];
                /*手机号隐藏中间四位（商会不隐藏）*/
//                foreach($result_info['list'] as &$value)
//                {
//                    $value['tel']=substr_replace($value['tel'],'****',3,4);
//                }
                $this->assign("list",$result_info['list']);
                return $this->fetch();
            }
        }

    }
    /*领导列表*/
    public function leaderList()
    {
        $id=input('id');
        $access_token=session::get("access_token");
        $data1=array(
            "module"=>'Bussiness',
            "interface"=>'1002',
            "version"=>'1.0.0',
            "data"=> [
                "bussiness_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("list",$result_info['list']);
            return $this->fetch();
        }
    }
    /*商会会员列表*/
    public function memberList()
    {
        $id=input('id');
        $access_token=session::get("access_token");
        $page=input('page');
        $page_size=input('page_size');
        $page=empty($page)?1:$page;
        $page_size=empty($page_size)?10:$page_size;;
        $data1=array(
            "module"=>'Bussiness',
            "interface"=>'1003',
            "version"=>'1.0.0',
            "data"=> [
                "page"=> $page,
                "page_size"=> $page_size,
                "bussiness_id"=> $id,
                "access_token"=> $access_token
            ]
        );

        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];

        if(request()->isAjax())
        {
            if($result_code!=="0000")
            {
                return 0;
            }else {
                $result_data = json_decode($res, true)['result_data']['list'];
                return json_encode($result_data);
            }
        }else{
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }else{
                $result_info=json_decode($res,true)['result_data'];
                $this->assign("list",$result_info['list']);
                $this->assign("bussiness_id",empty($id)?0:$id);
                return $this->fetch();
            }
        }

    }
    /*商会会员详情*/
    public function memberInfo()
    {
        $id=input('id');
        $access_token=session::get("access_token");
        $data1=array(
            "module"=>'Bussiness',
            "interface"=>'1004',
            "version"=>'1.0.0',
            "data"=> [
                "uid"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("info",$result_info['info']);
            return $this->fetch();
        }
    }
    /*手机号显示*/
    public function showMobile()
    {
        $id=input('uid');
        $access_token=session::get("access_token");
        $data1=array(
            "module"=>'Bussiness',
            "interface"=>'1006',
            "version"=>'1.0.0',
            "data"=> [
                "user_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }

        }else{
            $result_info=json_decode($res,true)['result_data'];

        }
        return json_encode(array('result_code'=>$result_code,'result_data'=>$result_info));

    }
    /*发送互通协议*/
    public function sendTreaty()
    {
        /*获取自己的信息*/
        $access_token=Session::get('access_token');
        $user=User::userFromToken($access_token);
        $recive_id=input('recive_id');
        if($user['bussiness_id']>=$recive_id)
        {
            $tag=$recive_id.",".$user['bussiness_id'];
        }else{
            $tag=$user['bussiness_id'].",".$recive_id;
        }
        $data=array(
            "sender_bus_id"=>$user['bussiness_id'],
            "receiver_bus_id"=>$recive_id,
            "tag"=>$tag,
            "status"=>0,
            "create_time"=>time(),
            "update_time"=>time(),
        );
        $id=Db::name('bussiness_agreement')
            ->insertGetId($data);
        if($id)
        {
            return 1;
        }else{
            return 0;
        }

    }
    /*商会动态列表*/
    public function bussinessNewsList()
    {
        $data=input();
        $page=empty($data['page'])?1:input('page');
        $page_size=empty($data['page_size'])?5:input('page_size');
        $category_id=empty($data['category_id'])?1:$data['category_id'];
        $access_token=Session::get("access_token");
        /*获取分类*/
        $category=Db::name('content_category')
            ->where('is_visible',1)
            ->order('display_order')
            ->select();
        $this->assign("category",$category);

        $data1=array(
            "module"=>'Member',
            "interface"=>'1010',
            "version"=>'1.0.0',
            "data"=> [
                "page_size"=> $page_size,
                "page"=> $page,
                "bussiness_id"=> $data['bussiness_id'],
                "category_id"=> $category_id,
                "access_token"=> $access_token
            ]
        );

        if(request()->isPost())
        {
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                return 0;
            }else{
                $result_data=json_decode($res,true)['result_data']['list'];
                return json_encode($result_data);
            }

        }else{
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }
            $result_data=json_decode($res,true)['result_data'];
            $this->assign("list",$result_data['list']);
            $this->assign("cid",$category_id);
            $this->assign("bussiness_id",$data['bussiness_id']);
            return $this->fetch();
        }

    }
}